package com.ch.swagger2.launch;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

/**
 * @author cj-ch
 * @date 2018/5/3 上午11:48
 */
@EnableWebSecurity
@Configuration
public class HttpSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http
                //禁用csrf避免出现Could not verify the provided CSRF token because your session was not found
                .csrf()
                .disable()
                .authorizeRequests()
                .antMatchers("/swagger-ui.html").hasRole("ADMIN")
                .anyRequest().permitAll()
                .and()

                // ...
                .formLogin();

    }



    @Override
    protected void configure(AuthenticationManagerBuilder auth)
            throws Exception {
        auth
                .inMemoryAuthentication()
                .withUser("user")
                .password("password")
                .roles("USER")
                .and()
                .withUser("adminr")
                .password("password")
                .roles("ADMIN","USER");
    }






}